﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vaffanculo.Data.Interfaces;
using System.Data.Common;
using System.Data.Objects;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Transactions;

namespace Vaffanculo.Data.Implements
{
    /// <summary>
    /// Unit of Work for Entity Framework.
    /// </summary>   
    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly IDbContext _context;

        /// <summary>
        /// Constructor Unit Of Work.
        /// </summary>
        /// <typeparam name="IDbContext">
        /// The context type that requires a UnitOfWork.
        /// </typeparam>
        public UnitOfWork(IDbContext context)
        {
            this._context = context;            
        }

        #region IUnitOfWork Members

        /// <summary>
        /// Obtain DbContext.
        /// </summary> 
        /// <returns>IDbContext</returns>
        public IDbContext GetIDbContext()
        {
            return _context;
        }

        /// <summary>
        /// Save changes to database.
        /// </summary> 
        public void Commit()
        {
            _context.SaveChanges();
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Dispose of connection or dataSet
        /// </summary> 
        public void Dispose()
        {
            if (_context != null)
            {
                _context.Dispose();
            }
            GC.SuppressFinalize(this);
        }

        #endregion
        
    }
}
